R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

## Warning: package 'plotly' was built under R version 3.6.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
## 
## Attaching package: 'plyr'
## The following objects are masked from 'package:plotly':
## 
##     arrange, mutate, rename, summarise
## Warning: package 'flexdashboard' was built under R version 3.6.3
## Warning: package 'sf' was built under R version 3.6.3
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
## Warning: package 'htmlwidgets' was built under R version 3.6.3

Static Plots

Column

library(reshape2)

names(gdb2)[names(gdb2)=="neoMR"]<- "Neonates"
names(gdb2)[names(gdb2)=="age1_5MR"]<- "Age 1 to 5 "
names(gdb2)[names(gdb2)=="postneoMR"]<- "Post Neonatles"
names(gdb2)[names(gdb2)=="under5MR"]<- "Under 5 years old"

gdbL=melt(gdb2, # all the data
           id.vars = 'gbdRegion') # unique id per row

library(ggplot2)
base = ggplot(data = gdbL, aes(x = variable,
                                y =gbdRegion)) 

heat1= base +  geom_tile(aes(fill = value)) 


#inverse color -1
heat2 = heat1 + scale_fill_gradient(low = 'grey',
                                    high = "black")

# change in REORDER
base= ggplot(data = gdbL, aes(x = reorder(variable, 
                                           value, median),
                               y =reorder(gbdRegion,
                                          value, median)))

plot7=base + geom_tile(aes(fill = value)) + labs(title = "Cases of Infants Mortality by World Bank Regions in 2010", caption ="Source :World Bank 2010") +   scale_fill_gradient(low = 'grey90',high = "grey50") +
    theme(legend.title=element_text(size=14),legend.text=element_text(size=13),plot.caption = element_text(vjust= -0.5, hjust = 1.7, size = 10, face = "bold"),plot.title = element_text(hjust = 0.5, size = 12, face = "bold"), axis.text.x = element_text(angle = 0, hjust = 0.5,vjust = 0.5,size = 11,face = 'bold'),
              axis.text.y = element_text(size = 12,face = 'bold'))+ labs(fill=' Infants Mortality cases per 100.K ',
                     x =NULL,
                     y = NULL)

plot7

Column

# absolute values
absoluteT=table(gdb$gbdRegion,
                exclude = 'nothing') #include all!




prop=prop.table(absoluteT)

ToPlot=prop*100
# as data frame
tableFreq=as.data.frame(ToPlot)
names(tableFreq)=c("Region","proportion")
library(ggplot2)
 base= ggplot(data = tableFreq, 
             aes(x = Region,
                 y = proportion))+ theme_classic()


plot1 = base + geom_bar(fill ="blue",
                        stat = 'identity') +
  coord_flip()

titleText='Distribution of countries by World Bank Regions'
sourceText='Source: World Bank'

plot2 = plot1 + labs(title=titleText,
                     x =NULL,
                     y = NULL,
                     caption = sourceText)

plot3 = plot2 + geom_hline(yintercept = 5.88, #where
                           linetype="dashed", 
                           size=1, #thickness
                           alpha=0.5) #transparency

library(scales) # for "unit_format""
## Warning: package 'scales' was built under R version 3.6.2
# customize Y axi
plot4 = plot3 + scale_y_continuous(breaks=c(0,2,4,6,8,10,12),
                                   limits = c(0,12), 
                                  labels=unit_format(suffix = '%')) 



plot5 = plot4 + theme(plot.caption = element_text(hjust = 1), 
                      plot.title = element_text(hjust = 0.5))

P=paste0(round(tableFreq$proportion,2), '%')

LABELS=P
plot6 = plot5 + geom_text(vjust=0.5, #hjust if flipping
                          size = 1,
                          aes(y = proportion ,
                              label = LABELS))

tableFreq=tableFreq[order(tableFreq$proportion),]


regionOrd=tableFreq[order(tableFreq$proportion),'Region']

LABELS=paste0(round(tableFreq$proportion,2), '%')

base= ggplot(data = tableFreq, 
             aes(x = region,
                 y = proportion)) 


base= base + scale_x_discrete(limits=regionOrd) 

base= base +theme_classic()
   

base= ggplot(data = tableFreq, 
             aes(x = Region,
                 y = proportion))
base = base + scale_x_discrete(limits=regionOrd)


plot1 = base + geom_bar(fill ="blue",
                        stat = 'identity') +
  coord_flip()


titleText='Distribution of countries by World Bank Regions'
sourceText='Source: World Bank'

plot2 = plot1 + labs(title=titleText,
                     x =NULL,
                     y = NULL,
                     caption = sourceText)


plot3 = plot2 


plot4 = plot3 + scale_y_continuous(breaks=c(0,2,4,6,8,10,12),
                                   limits = c(0,13), 
                                   labels=unit_format(suffix = '%')) 


plot5 = plot4 + theme(plot.caption = element_text(hjust = 1,face = 'bold', size = 10), 
                      plot.title = element_text(hjust = 0.5, size = 15, face = 'bold'))+ theme(axis.text.x = element_text(angle = 0, 
                                         hjust = 0.55,
                                         size = 15, face = "bold"),
              axis.text.y = element_text(size = 13, face = "bold"))


plot6 = plot5 + geom_text(vjust=0.5,hjust=-0.1, #hjust if flipping
                          size = 5,
                          aes(y = proportion ,
                              label = LABELS))
plot6 #+ coord_flip() # wanna flip the plot?

row

linkCSV='https://github.com/pubpolicy/PubPolicy-543/raw/main/gbdChildMortality_2010s.csv'

dataCSV=read.csv(linkCSV)

location="https://github.com/pubpolicy/PubPolicy-543/raw/main/"
file="WB_countries_Admin0_10m.json"

linkToFile=paste0(location,file)

# import
library(sf)

mapWorld=read_sf(linkToFile)

mapWorldVars=merge(mapWorld, #map first
                   dataCSV, 
                   by.x='ISO_A3', by.y='iso')

library(ggplot2)
# plot original map
base=ggplot(data=mapWorld) + geom_sf(fill='grey90',
                                     color=NA) + theme_classic()

titleText='Distribution of countries by World Bank Economy Classification'
sourceText='Source: World Bank'
colMap= base + geom_sf(data=mapWorldVars,
                       aes(fill=ECONOMY),
                       color=NA) + labs(title=titleText,
                     x =NULL,
                     y = NULL,
                     subtitle = "Source: World Bank 2010")+ theme(legend.title.align = 0.5,legend.title=element_text(margin = margin(t = 10),size=10, face = "bold"),legend.text=element_text(size=10, face = "bold"),plot.caption = element_text(hjust = 2,face = 'bold',size = 10), 
                      plot.title = element_text(hjust = 1, size = 15,face = "bold"),legend.key.size = unit(0.7, "cm"))+ theme(axis.text.x = element_text(angle = 0, 
                                         hjust = 1,
                                         size = 15, face = "bold"),
              axis.text.y = element_text(size = 15, face = "bold"))


colMap

Interactive Plots

Column

library(htmlwidgets)
base= ggplot(data = gdbL, aes(x = reorder(variable, 
                                           value, median),
                               y =reorder(gbdRegion,
                                          value, median)))


plot10 =base + geom_tile(aes(fill = value)) + labs(title = "Cases of Infants Mortality by World Bank Regions in 2010", caption ="Source :World Bank 2010") 
Plot11 = plot10 +   scale_fill_gradient(low = 'grey90',high = "grey50") 

plot12 = Plot11 + theme(legend.title=element_text(size=0),legend.text=element_text(size=0),plot.caption = element_text(hjust = 1.3, size = 7, face = "bold"),plot.title = element_text(hjust = 0.5, size = 11, face = "bold"), axis.text.x = element_text(angle = 0, hjust = 0.5,vjust = 0.5,size = 10,face = 'bold'),
              axis.text.y = element_text(size = 8,face = 'bold'))
plot7= plot12 +  labs(fill=' Infants Mortality cases in 2010 ',
                     x =NULL,
                     y = NULL)
plot7%>%ggplotly()
## Warning in matrix(g$fill_plotlyDomain, nrow = length(y), ncol = length(x), :
## data length [748] is not a sub-multiple or multiple of the number of rows [21]
## Warning in matrix(g$hovertext, nrow = length(y), ncol = length(x), byrow =
## TRUE): data length [748] is not a sub-multiple or multiple of the number of rows
## [21]

row

plot5 = plot4 + theme(plot.caption = element_text(hjust = 3,face = 'bold', size = 8), 
                      plot.title = element_text(hjust = 0.5, size = 15, face = 'bold'))+ theme(axis.text.x = element_text(angle = 0, 
                                         hjust = 3,
                                         size = 12, face = "bold"),
              axis.text.y = element_text(size = 13, face = "bold"))


plot7 = plot5 + geom_text(vjust=0.5,hjust= 4, #hjust if flipping
                          size = 4,
                          aes(y = proportion ,
                              label = LABELS))
 #+ coord_flip() # wanna flip the plot?
plot7%>%ggplotly()

row

library(ggplot2)
library(plotly)
library(plyr)
library(flexdashboard)
library(sf)
library(htmlwidgets)

linkCSV='https://github.com/pubpolicy/PubPolicy-543/raw/main/COVID-19%20cases%20and%20testing%20by%20County.csv'

dataCSV=read.csv(linkCSV)



location="https://github.com/pubpolicy/PubPolicy-543/raw/main/"
file="WA_County_Boundaries.json"

linkToFile=paste0(location,file)

# import
library(sf)


mapCounty=read_sf(linkToFile)

mapWorldVars=merge(mapCounty, #map first
                   dataCSV, 
                   by.x='JURISDIC_2',by.y='JURLBL')


library(ggplot2)
# plot original map
base=ggplot(data=mapCounty) + geom_sf(fill='grey90',
                                     color=NA) + theme_classic()

titleText='COVID-19 testing rates in Washington State by county'
sourceText='Source: Washington Department of Natural Resource'
colMap= base + geom_sf(data=mapWorldVars,
                    aes(fill= Average.daily.COVID.19.testing.rate.per.100K.people),
                       color='black') + labs(title=titleText,subtitle = sourceText,
                     x =NULL,
                     y = NULL,
                     caption = sourceText)+ scale_fill_gradient(low = 'blue',
                             high= 'red')
colMap2= colMap + theme(legend.title=element_text(size=10, face = 'bold'),plot.title = element_text(vjust = -80,  hjust = 10, size = 13, face = "bold"))


colMap2%>%ggplotly()

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.